package repositorios;

 
import interfaces.IRepositorioReclamacao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import modelos.Linha;
import modelos.Reclamacao;
import modelos.Usuario;
import util.GerenteConexao;


public class RepositorioReclamacao implements IRepositorioReclamacao{
    
    public void adicionarReclamacao(Reclamacao r){
        String sql = "insert into reclamacao( id, id_cliente, id_linha, data, descricao, observacao) values(?,?,?,?,?,?)";
        
        try {
            PreparedStatement ps = GerenteConexao.getConexao().prepareStatement(sql);
            montarOperacao(ps, r);
            
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void removerReclamacao(Reclamacao r){
        String sql = "delete from reclamacao where id = ?";
        try{
            PreparedStatement ps = GerenteConexao.getConexao().prepareStatement(sql);
            ps.setLong(1, r.getId());
            
            ps.executeUpdate();
            
        } catch(Exception e){
            e.printStackTrace();
        }
    }
    
    public List<Reclamacao> buscarDescricaoLinha(String descricao){
    	
    	String sql = "Select r.* FROM reclamacao r, linha l WHERE l.descricao like '" + descricao + "'" +
    					"AND l.id = r.id_linha";
        List<Reclamacao> retorno = null;
        try {
            Statement st = GerenteConexao.getConexao().createStatement();
            ResultSet rs = st.executeQuery(sql);
            retorno = new ArrayList<Reclamacao>();
            
            while(rs.next()){
            	Reclamacao r = montarObjeto(rs);
            	retorno.add(r);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retorno;
    }
    
    public List<Reclamacao> buscaLike(String campo, String valor){
		
		String sql = "Select * FROM reclamacao WHERE " + campo + " like '%" + valor + "%'";
		List<Reclamacao> retorno = null;
		try {
			Statement st = GerenteConexao.getConexao().createStatement();
			ResultSet rs = st.executeQuery(sql);
			retorno = new ArrayList<Reclamacao>();

			while(rs.next()){
				Reclamacao r = montarObjeto(rs);
				retorno.add(r);
			}
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
		return retorno;
	}
    
    public List<Reclamacao> listarReclamacao(){
    	String sql = "Select * from reclamacao";
        List<Reclamacao> retorno = null;
        try {
            Statement st = GerenteConexao.getConexao().createStatement();
            ResultSet rs = st.executeQuery(sql);
            retorno = new ArrayList<Reclamacao>();
            
            while(rs.next()){
            	Reclamacao r = montarObjeto(rs);
            	retorno.add(r);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retorno;
    }
    
    public void editarReclamacao(Reclamacao r){
        String sql = "update reclamacao set id=?, id_cliente=?, id_linha=?, data =?, descricao =?, observacao =? where id =" + r.getId();
        
        try {
            PreparedStatement ps = GerenteConexao.getConexao().prepareStatement(sql);
            montarOperacao(ps, r);
            
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private Reclamacao montarObjeto(ResultSet rs){
		Reclamacao r = new Reclamacao();
		Linha l = new Linha();
		Usuario u = new Usuario();
		try {
        	
        	r.setId(rs.getInt("id"));
        	l.setId(rs.getInt("id_linha"));
        	r.setLinha(l);
        	u.setId(rs.getInt("id_cliente"));
        	r.setUsuario(u);
        	r.setData(rs.getString("data"));
        	r.setDescricao(rs.getString("descricao"));
        	r.setObservacao(rs.getString("observacao"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return r;
	}
    
    private void montarOperacao(PreparedStatement ps, Reclamacao r){

		try {
			ps.setLong(1, r.getId());
			ps.setLong(2, r.getUsuario().getId());
			ps.setLong(3, r.getLinha().getId());
			ps.setString(4, r.getData());
            ps.setString(5, r.getDescricao());
            ps.setString(6, r.getObservacao());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
